package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type BusinessCertificateHandler interface {
	ListBusinessCertificate() gin.HandlerFunc
	GetBusinessCertificate() gin.HandlerFunc
	AddBusinessCertificate() gin.HandlerFunc
	EditBusinessCertificate() gin.HandlerFunc
	DeleteBusinessCertificate() gin.HandlerFunc
	BatchDeleteBusinessCertificate() gin.HandlerFunc
}

type businessCertificateHandler struct {
	somsClient soms.Client
}

func (h *businessCertificateHandler) ListBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListBusinessCertificateDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListBusinessCertificate(util.GetRPCContext(ctx), &api.ListBusinessCertificateRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.BusinessCertificateData{
				Name: req.Name,
	Code: req.Code,
	Remark: req.Remark,
	Attachment: req.Attachment,
	DueDate: req.DueDate,
	ImpendingReminderDays: req.ImpendingReminderDays,
	Status: req.Status,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *businessCertificateHandler) GetBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetBusinessCertificate(util.GetRPCContext(ctx), &api.BusinessCertificateData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *businessCertificateHandler) AddBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.BusinessCertificateData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.BusinessCertificateData{
            Id:         data.Id,
            Name: data.Name,
	Code: data.Code,
	Remark: data.Remark,
	Attachment: data.Attachment,
	DueDate: data.DueDate,
	ImpendingReminderDays: data.ImpendingReminderDays,
	Status: data.Status,
	
        }
		err := h.somsClient.AddBusinessCertificate(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *businessCertificateHandler) EditBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.BusinessCertificateData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.BusinessCertificateData{
            Id:         id,
            Name: data.Name,
	Code: data.Code,
	Remark: data.Remark,
	Attachment: data.Attachment,
	DueDate: data.DueDate,
	ImpendingReminderDays: data.ImpendingReminderDays,
	Status: data.Status,
	
        }
		if err = h.somsClient.EditBusinessCertificate(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *businessCertificateHandler) DeleteBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteBusinessCertificate(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *businessCertificateHandler) BatchDeleteBusinessCertificate() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteBusinessCertificateRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteBusinessCertificate(util.GetRPCContext(ctx), &api.BatchDeleteBusinessCertificateRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

